/*
* 私募产品列表 model
*privatePdt.js*/

import {routerRedux} from 'dva/router';
import api from '../api';
import Mock from 'mockjs';

export default {
	namespace : 'privatePdt',
	state : {
		
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({pathname, query}) => {
				if (pathname === '/match/matchProduct') {
					dispatch({
						type:'getPrivatePdt',
						payload:{
							'productType':'SIMU',
							'currentpage':1,
							'pagesize':15,
						}
					})
					dispatch({
						type: 'getsmmcAlxr',
					})
				}
			})
		}
	},
	effects : {
		*getsmmcAlxr({payload},{call,put}){
			const response = yield call(api.privateMgt.getsmmcAlxr, payload);
			yield put({
        type: 'getsmmcAlxrSuccess',
        payload: { response },
      });
		},
		*getPrivatePdt({payload},{call,put}){
			const response = yield call(api.privateMgt.getPrivatePdt, payload);
			yield put({
        type: 'getPrivatePdtSuccess',
        payload: { response },
      });
		},
		*addPrivatePdt({payload},{call,put}){
			const response = yield call(api.privateMgt.addPrivatePdt, payload);
			yield put({
        type: 'getPrivatePdt',
        payload:{
					'productType':'SIMU',
					'currentpage':1,
					'pagesize':15,
				}
      });
    },
		*deletePrivatePdt({payload},{call,put}){
			const productId = payload.productId
			const response = yield call(api.privateMgt.deletePrivatePdt, productId,{...payload});
			yield put({
        type: 'getPrivatePdt',
        payload:{
					'productType':'SIMU',
					'currentpage':1,
					'pagesize':15,
				}
      });
		},
		*editPrivatPdt({payload},{call,put}){
			// console.log('dddddddd',payload)
			const productId = payload.id;
			const response = yield call(api.privateMgt.editPrivatPdt,productId,{...payload});
			yield put({
        type: 'getPrivatePdt',
        payload:{
        	'productType':'SIMU',
					'currentpage':1,
					'pagesize':15,
				}
      });
		}
	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		getsmmcAlxrSuccess(state,{payload}){
			const {data,} = payload.response
      // console.log("getsmmcAlxrSuccess>>>",data);
      const managerArr = [];
			for (let i = 0; i < data.length; i++) {
		    let obj = {
		      id: data[i].id,
		      name: data[i].name,
		    };
		    managerArr.push(obj);
		 	}
      // console.log('getsmmcAlxrSuccess',managerArr)
      return {
        ...state,
        managerData:managerArr,
        managerList:data
      }
		},
		getPrivatePdtSuccess(state,{payload}){
			const {data,page} = payload.response
      // console.log("getPrivatePdtSuccess>>>",data);
      return {
        ...state,
        pdtData:data,
        page:page
      }
		},
	}
};
